package wpwork.service;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import wpwork.redisEntity.*;

@Service
@Slf4j
public class RedisPublishService {

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    @Autowired
    private RedisQueueConfig queueConfig;

    /**
     * 发布人员信息
     */
    public boolean publishPersonInfo(PersonInfoRequest personInfo) {
        try {
            redisTemplate.convertAndSend(queueConfig.PERSON_INFO_QUEUE, personInfo);
            log.info("人员信息发布成功, serial: {}, tenant: {}",
                    personInfo.getF_serial(), personInfo.getF_tenant_id());
            return true;
        } catch (Exception e) {
            log.error("人员信息发布失败: {}", e.getMessage(), e);
            return false;
        }
    }

    /**
     * 发布位置查询请求
     */
    public boolean publishLocationRequest(LocationRequest locationRequest) {
        try {
            redisTemplate.convertAndSend(queueConfig.LOCATION_REQUEST_QUEUE, locationRequest);
            log.info("位置查询请求发布成功, serial: {}", locationRequest.getF_serial());
            return true;
        } catch (Exception e) {
            log.error("位置查询请求发布失败: {}", e.getMessage(), e);
            return false;
        }
    }

    /**
     * 发布位置查询响应
     */
    public boolean publishLocationResponse(LocationResponse locationResponse) {
        try {
            redisTemplate.convertAndSend(queueConfig.LOCATION_RESPONSE_QUEUE, locationResponse);
            log.info("位置查询响应发布成功, serial: {}", locationResponse.getF_serial());
            return true;
        } catch (Exception e) {
            log.error("位置查询响应发布失败: {}", e.getMessage(), e);
            return false;
        }
    }

    /**
     * 发布轨迹查询请求
     */
    public boolean publishTrackRequest(TrackRequest trackRequest) {
        try {
            redisTemplate.convertAndSend(queueConfig.TRACK_REQUEST_QUEUE, trackRequest);
            log.info("轨迹查询请求发布成功, serial: {}", trackRequest.getF_serial());
            return true;
        } catch (Exception e) {
            log.error("轨迹查询请求发布失败: {}", e.getMessage(), e);
            return false;
        }
    }

    /**
     * 发布轨迹查询响应
     */
    public boolean publishTrackResponse(TrackResponse trackResponse) {
        try {
            redisTemplate.convertAndSend(queueConfig.TRACK_RESPONSE_QUEUE, trackResponse);
            log.info("轨迹查询响应发布成功, serial: {}", trackResponse.getF_serial());
            return true;
        } catch (Exception e) {
            log.error("轨迹查询响应发布失败: {}", e.getMessage(), e);
            return false;
        }
    }

    /**
     * 发布AI外呼请求
     */
    public boolean publishAICallRequest(AICallRequest aiCallRequest) {
        try {
            redisTemplate.convertAndSend(queueConfig.AI_CALL_QUEUE, aiCallRequest);
            log.info("AI外呼请求发布成功, serial: {}, message: {}",
                    aiCallRequest.getF_serial(), aiCallRequest.getF_message());
            return true;
        } catch (Exception e) {
            log.error("AI外呼请求发布失败: {}", e.getMessage(), e);
            return false;
        }
    }
}